<?php
/**
 * //w/w/w.s/h/u/o/t//u/p/u c/o/m  说/图/谱/源/码/分/享
 */
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/26 0026
 * Time: 下午 4:50
 */
defined("IN_IA")or exit("Access Denied");
checklogin();  //验证是否登录
global $_W,$_GPC;
$uniacid=$_W['uniacid'];
$op=$_GPC['op'] ? $_GPC['op'] : "vet_set";

if($op=='index'){
    $condition=array();
    $condition['uniacid']=$uniacid;
    $listCount=pdo_getall("cqkundian_farm_vet",$condition);
    $total=count($listCount);   //数据的总条数
    $pageSize=10; //每页显示的数据条数
    $pageIndex=intval($_GPC['page']) ? intval($_GPC['page']) :1;  //当前页
    $pager=pagination($total,$pageIndex,$pageSize);
    $list=pdo_getall("cqkundian_farm_vet",$condition,'','','rank asc',array($pageIndex,$pageSize));
    include $this->template("web/vet/index");
}

if($op=='vet_edit'){
    if($_GPC['id']){
        $list=pdo_get('cqkundian_farm_vet',array('id'=>$_GPC['id'],'uniacid'=>$uniacid));
        $list['certificate']=unserialize($list['certificate']);
    }
    include $this->template("web/vet/edit");
}

if($op=='vet_save'){
    $data=array(
        'head_img'=>tomedia($_GPC['head_img']),
        'name'=>$_GPC['name'],
        'uid'=>$_GPC['uid'],
        'introduce'=>$_GPC['introduce'],
        'status'=>$_GPC['status'],
        'rank'=>$_GPC['rank'],
        'uniacid'=>$uniacid,
    );
    $certificate=$_GPC['certificate'];
    for ($i=0;$i<count($certificate);$i++){
        $certificate[$i]=tomedia($certificate[$i]);
    }
    $data['certificate']=serialize($certificate);
    if(empty($_GPC['id'])){  //新增
        $request=pdo_insert("cqkundian_farm_vet",$data);
    }else{
        $condition=array(
            'id'=>$_GPC['id'],
            'uniacid'=>$uniacid,
        );
        $request=pdo_update("cqkundian_farm_vet",$data,$condition);
    }
    if($request){
        message("操作成功",url('site/entry/vet',array('m'=>'kundian_farm','op'=>'index')));
    }else {
        message("操作失败", '', 'warning');
    }
}

if($op=='statusChange'){
    $id=$_GPC['id'];
    $res=pdo_update('cqkundian_farm_vet',array('status'=>$_GPC['status']),array('id'=>$id,'uniacid'=>$uniacid));
    echo $res ? json_encode(array('status'=>1)) : json_encode(array('status'=>2));die;
}

if($op=='delete'){
    $id=$_GPC['id'];
    $res=pdo_delete('cqkundian_farm_vet',array('id'=>$id,'uniacid'=>$uniacid));
    echo $res ? json_encode(array('status'=>1)) : json_encode(array('status'=>2));die;
}

//兽医展示设置
if($op=='vet_set'){
    $condition=array(
        'ikey'=>array('vet_icon','vet_banner','vet_title','vet_english_title','is_open_vet'),
        'uniacid'=>$uniacid,
    );
    $list=pdo_getall('cqkundian_farm_set',$condition);
    $nowList=array();
    foreach ($list as $key => $value) {
        $nowList[$value['ikey']]=$value['value'];
    }
    include $this->template('web/vet/vet_set');
}

//保存兽医展示信息
if($op=='vet_set_save'){
    $data=$_POST;
    $res=0;
    $data['vet_icon']=tomedia($data['vet_icon']);
    $data['vet_banner']=tomedia($data['vet_banner']);
    if(empty($data['is_open_vet'])){
        $data['is_open_vet']=0;
    }
    foreach ($data as $key=>$v){
        $updateData=array(
            'ikey'=>$key,
            'value'=>$v,
            'uniacid'=>$uniacid,
        );
        $cond=array(
            'ikey'=>$key,
            'uniacid'=>$uniacid,
        );
        $farmData=pdo_get('cqkundian_farm_set',$cond);
        if(empty($farmData)){
            $res+=pdo_insert('cqkundian_farm_set',$updateData);
        }else{
            $res+=pdo_update('cqkundian_farm_set',$updateData,$cond);
        }
    }
    if($res){
        message('操作成功',url('site/entry/vet',array('m'=>'kundian_farm','op'=>'vet_set')));die;
    }else{
        message('操作失败');
    }
}